Global Event Rule api error - unsupported media type

I am working on a python script to create global event rules using pagerduty’s rest api v2 and am running into some issues just trying to get started.

################ create-eventrule.py #################

Response Status: 415
b’{“error”:“Unsupported Media Type”}’

Here is my data payload:

Data payload

data = {“condition”: [“and”,[“contains”,[“path”,“payload”,“source”],“website”]], “actions”: [[“route”,“test”],[“suppress”,False],[“severity”,“info”]]}

If you could please send us the script you’re trying to use, we’d be happy to take a look at it and help determine what the issue is.

Here is the script I have thus far

#! /usr/bin/python3

import requests


authorization_token = '##########################'
pagerduty_session = requests.Session()
pagerduty_session.headers.update({
  'Authorization': 'Token token=' + authorization_token,
  'Accept': 'application/vnd.pagerduty+json;version=2'
})

# PagerDuty API Endpoint for event rules
url = 'https://api.pagerduty.com/event_rules'

# Data payload
data = {"condition": ["and",["contains",["path","payload","source"],"website"]], "actions": [["route","test"],["suppress",False],["severity","info"]]}
print(data)
print(type(data))

# Response
response = pagerduty_session.post(url, data=data)

print("\n################ create-eventrule.py #################\n")

# Print status and body of response
print('Response Status:', response.status_code)
if response.status_code != 200:
    print(response.content)
    raise SystemExit(1)

Hi Shaun,

The original response error indicates a 415 HTTP error.

This usually indicates an unsupported media type was sent in the API request, as the linked guide mentions:

The format problem might be due to the request’s indicated Content-Type or Content-Encoding

Our Global Event Rules documentation indicates that you must include Content-Type: application/json in the API request, which I saw missing in your API request.

Could you please see if you receive the 415 error after adding the Content-Type header.

Cheers,

That seems to have helped now I think i just need to adjust the payload i am sending

{'condition': ['and', ['contains', ['path', 'payload', 'source'], 'website']], 'actions': [['route', 'P2KGCOP'], ['suppress', False], ['severity', 'info']]}
<class 'dict'>

################ create-eventrule.py #################

Response Status: 400
b'{"error":"Bad Request"}'

updated code

#! /usr/bin/python3

import requests


authorization_token = '##########################'
pagerduty_session = requests.Session()
pagerduty_session.headers.update({
  'Authorization': 'Token token=' + authorization_token,
  'Content-Type': 'application/json',
  'Accept': 'application/vnd.pagerduty+json;version=2'
})

# PagerDuty API Endpoint for event rules
url = 'https://api.pagerduty.com/event_rules'

# Data payload
data = {"condition": ["and",["contains",["path","payload","source"],"website"]], "actions": [["route","P2KGCOP"],["suppress",False],["severity","info"]]}
print(data)
print(type(data))

# Response
response = pagerduty_session.post(url, data=data, headers=pagerduty_session.headers)

print("\n################ create-eventrule.py #################\n")

# Print status and body of response
print('Response Status:', response.status_code)
if response.status_code != 200:
print(response.content)
raise SystemExit(1)

Let us know if we can be of further assistance!

Hi Shaun,

A payload could be as simple as follows:

"condition": [
                "or",
                [
                    "exists",
                    [
                        "path",
                        "headers",
                        "from",
                        "0",
                        "address"
                    ]
                ]
            ],
            "catch_all": false,
            "advanced_condition": [],
            "actions": [
                [
                    "route",
                    "[PUT_SERVICE_ID_HERE"
                ],
                [
                    "suppress",
                    true
                ],
                [
                    "annotate",
                    "meow"
                ]
            ]
        }, 
1 Like

Thanks Malcom that helps a lot!

Ok guys the trick was changing the request parameters

response = pagerduty_session.post(url, json=data2, headers=pagerduty_session.headers)

Instead of using the data paramater, I used the json parameter for the requests.post() method which did create the global event rule!

1 Like

Hi Shaun,

Happy to hear that did the trick! Feel free to let us know if you have any other questions.

Best,

Malcolm, does this section need to be formatted differently “catch_all”: false,
I get the following error - NameError: name ‘false’ is not defined

Is there a way to order the rule when it is created? Ex. Add it to the top of the existing rules?

Figured it out. false needs to be False…case sensitive.